package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.model.dto.h5.UserLoginDto;
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.atguigu.spzx.service.utils.AuthContextUtils;
import com.atguigu.spzx.user.service.UserInfoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserInfoController
 * Package: com.atguigu.spzx.user.controller
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/16 4:11
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;


    /**
     *  /api/user/userInfo/auth/getCurrentUserInfo
     *  获取用户信息
     * @param token
     * @return
     */
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(@RequestHeader(value = "token",required = false) String token){
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登录
     * @param userLoginDto
     * @param request
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request){
        String token = userInfoService.login(userLoginDto,request);
        return Result.build(token,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户注册
     * @param userRegisterDto
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
